<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.wef.common.util.CommonMessageDomain"%>
<%@page import="com.gfa4j.query.Field"%>
<%@page import="com.wef.system.entity.domain.UserDomain"%>
<%@page import="com.wef.system.entity.domain.RoleDomain"%>
<%@ include file="/pages/common/taglib.jsp"%>
<html>
	<head>
		<title></title>
		<%@include file="/styles/css_lib.jsp"%>
		<%@include file="/scripts/widgets/ext-2.2.1/css_lib.jsp"%>
		<%@include file="/scripts/widgets/gfa4ext/css_lib.jsp"%>
		<%@include file="/scripts/widgets/autoSelect/css_lib.jsp"%>
		
		<%@include file="/pages/common/js_global.jsp"%>
		<%@include file="/scripts/sdk/js_lib.jsp"%>
		<%@include file="/scripts/config/js_lib.jsp"%>
		<%@include file="/scripts/utils/js_lib.jsp"%>
		<%@include file="/scripts/i18n/js_lib.jsp"%>
		<%@include file="/scripts/widgets/ext-2.2.1/js_lib.jsp"%>
		<%@include file="/scripts/widgets/gfa4ext/js_lib.jsp"%>
		<%@include file="/scripts/widgets/autoSelect/js_lib.jsp"%>
		<script type="text/javascript">
			function roleEdit(){
				var grid=Ext.getCmp('divGrid1');
				var rec = grid.selModel.getSelected();
				if(!rec){
		    		Ext.Msg.alert(i18n.message.prompt, system.Role.selectedUser);
		    		return false;
		    	}
				Ext.getCmp('winUserCode').setValue(rec.data.userCode);
				Ext.getCmp('winUserName').setValue(rec.data.userName);
		    	winRole.show();
		    	var gridRole=Ext.getCmp('gridRole');
		    	setTimeout(function(){
			    	gridRole.store.load();
		    	}, 10);
			}
			function renderRoleType(value){
				switch(value){
					case '1': return 'Active';
					case '2': return 'Lock';
					case '3': return 'Disable';
				}
			} 
			function winOnRender(gridRole){   	
				// 取出选中用户已拥有的角色
				gridRole.store.on('load', function(){
					var userGrid=Ext.getCmp('divGrid1');
					var user = userGrid.selModel.getSelected().data;
			    	RoleUserService.getUserRoleByUserId(user.userId, {
			    		callback : function(results){
			    			if(!results.length > 0){
			    				return;
			    			}
				    		gridRole.selModel.clearSelections();
			    			var _store = gridRole.store;
			    			var is = [];
			    			Ext.each(results, function(userRole){
			    				var index = _store.find('roleId', userRole.role.roleId);
			    				if(index >= 0){
			    					is.push(index);
			    				}
				    		});
							gridRole.selModel.selectRows(is);
			    		},
			    		async : false
			    	});
				});
		
			    Ext.getCmp('winRoleBtnCancel').on('click', function(){		    	
			    	winRole.hide();
			    });	
		
			    Ext.getCmp('winRoleBtnOK').on('click', function(){
			    	var userGrid = Ext.getCmp('divGrid1');
			    	var userId = userGrid.selModel.getSelected().data.userId;
			    	var selectedRole = gridRole.selModel.getSelections();
			    	var roleIds = [];
			    	Ext.each(selectedRole, function(role){
			    		roleIds.push(role.data.roleId);
			    	});
			    	RoleUserService.saveRoleUsers(userId,roleIds,function(result){
			    		alert(String.format(system.Role.success));
			    	});
			    	winRole.hide();
			    });   	
			}
			function beforeSaveFuncValidateGridEditForm(){
				var flg = validateUserGridEditForm(document.getElementById('userGridEditForm'));
				if(!flg) {
					return false;
				}else {
					return true;
				}
			}
			$(window).ready(function(){
				$("#comboxOffice").autoSelect({
					url :"${ctx}",
					className:"com.wef.system.entity.Office",     //映射model
					displayField:"officeCode,officeName,address",						//显示列
					displayFieldI18n: "system.Office.officeCode,system.Office.officeName,system.Office.address",  //显示字段国际化,不需要可不设置
					condition:Gfa.GlobalConstant.CONDITION_LIKE_ANYWHERE_IC,		//查询条件
					width:450,														//总宽度，除以displayField应为整数 
					displayNum:8,													//显示行数
					canNotInput:true,												//不允许手输入，必须从下列列表中选择一条
					autoFill:"comboxOffice|{officeCode}",	//填充 到指定的控件
					queryField:"officeCode,officeName,address"
				});
			});
		</script>
		<ge:body>
			<ge:window id="win">
				<ge:grid.baseGrid 
					queryObjectName="com.wef.system.entity.User"
					id="divGrid1"
					readerFields="userId,userCode,userName,password,officeCode,firstName,middleName,lastName,position,mobile,fax,email,userType"  
					searchFormId="userSearchForm"
					searchBtnId="btnSearch"
					newBtnId="btnNew"
					saveBtnId="btnSave"
					deleteBtnId="btnDelete"
					cancelBtnId="btnCancel"
					editFormId="userGridEditForm"
					dwrService="UserService"
					dwrMethod="saveOrUpdateUsers"
					loadMask="true"
					bodyStyle="width:100%"
					autoWidth="true"	
				
					allowSelectPageSize="true"
					showExportToExcel="false"
					showExportAllToExcel="false"
					beforeSaveFunc="beforeSaveFuncValidateGridEditForm"	
					beforeNewRowFunc="beforeSaveFuncValidateGridEditForm"
					beforeRowSelectFunc="beforeSaveFuncValidateGridEditForm"	
					height="250"
					renderTo="divGrid1">
					<ge:grid.columnModel>
						<ge:grid.rowNumberer />
						<ge:grid.column header="userId" dataIndex="userId" hidden="true"/>
						<ge:grid.column header="system.User.userName" dataIndex="userName" width="100"/>
						<ge:grid.column header="system.User.officeCode" dataIndex="officeCode" width="100"/>
						<ge:grid.column header="system.User.position" dataIndex="position" width="100"/>
						<ge:grid.column header="system.User.mobile" dataIndex="mobile" width="100"/>
						<%-- <ge:grid.column header="system.User.fax" dataIndex="fax" width="100" /> --%>
						<ge:grid.column header="system.User.email" dataIndex="email" width="120" />
						<ge:grid.column header="system.User.userType" dataIndex="userType" width="120" />
						<ge:grid.column header="system.Role.Role" width="50" renderer="function(){return '<a href=\"javascript:roleEdit();\" ></ge:grid.column><img src=\"${ctx}/images/icon/change.gif\" /></a>'}" />
					</ge:grid.columnModel>

				</ge:grid.baseGrid>
			</ge:window>
			
			<!-- role window start -->
			<ge:window id="winRole" title="system.Role.Role"  closeAction="hide" width="540" height="400">
					<ge:form.fieldset title="Label">
						<ge:form.formPanel border="0" id="winRoleUserInfoPanel" layout="column" layoutConfig="2" bodyStyle="background-color:#DFE8F6;">
							<ge:panel layout="form" border="0" bodyStyle="background-color:#DFE8F6;margin-right:10px;" >
								<ge:form.textField id="winUserCode" readOnly="true" fieldLabel="system.User.userCode" name="userCode" disabled="true"/>
							</ge:panel>
							<ge:panel layout="form" border="0" bodyStyle="background-color:#DFE8F6;">
								<ge:form.textField id="winUserName" readOnly="true" fieldLabel="system.User.userName" name="userName" disabled="true"/>
							</ge:panel>
						</ge:form.formPanel>
					</ge:form.fieldset>
				<ge:grid.baseGrid 
					queryObjectName="com.wef.system.entity.Role"
					loadMask="true" 
					id="gridRole"
					bodyStyle="width:100%;height:100%"
					width="520"
					height="220"
					paging="true"
					onRender="winOnRender(this)"
					enableHdMenu="false"
					cancelBtnId="winRoleBtnCancel"
					selectFirstRowOnLoad="false" 
					readerFields="roleName,roleDesc,activeDate,expiredDate,roleType"
					>
					<ge:grid.columnModel>
						<ge:grid.rowNumberer />
						<ge:grid.column header="system.Role.RoleCode" dataIndex="roleName">
						</ge:grid.column>
						<ge:grid.column header="system.Role.RoleName" dataIndex="roleDesc" width="300" >
						</ge:grid.column>
						<ge:grid.column header="system.Role.activeDate" dataIndex="activeDate" renderer="Ext.util.Format.dateRenderer('Y-M-d')">
						</ge:grid.column>
						<ge:grid.column header="system.Role.expiredDate" dataIndex="expiredDate" renderer="Ext.util.Format.dateRenderer('Y-M-d')">
						</ge:grid.column>
						<ge:grid.column header="system.Role.UserType" dataIndex="roleType" renderer="renderRoleType" >
						</ge:grid.column>
					</ge:grid.columnModel>
				</ge:grid.baseGrid>
				<ge:form.formPanel border="0">
					<ge:button id="winRoleBtnOK" text="i18n.global.save" ></ge:button>
					<ge:button id="winRoleBtnCancel" text="i18n.global.cancel"></ge:button>
				</ge:form.formPanel>
			</ge:window>
			<!-- role window end -->
		</ge:body>
	</head>
	<body>
	<!--Content-->
	  <div id="Content">
		<!--Right-->
		<div class="ModuleSearch">
		    <div class="ModuleSearchBorder">
		      <div class="ModuleSearchTitle"><g:label showColon="false" text="<%= CommonMessageDomain.TITLE_SERACH %>" /></div>
				<!--搜索模块-->
				<html:form action="/system/user.do?method=index" styleId="userSearchForm">
				  <div class="ModuleSearchInput" id="topDiv">
					<table width="100%" cellspacing="0" cellpadding="0">
						<tr>
							<td width="60" align="left" ><g:label text="<%=UserDomain.userName_full %>" /></td>
							<td width="100" align="left"><g:text property="userName" condition="<%=Field.CONDITION_EQ%>" /></td>
				
							
							<td >&nbsp;</td>
						</tr>
					</table>
					</div>
					<!--按钮-->
		          	<div class="ModuleEditButton" id="buttonDiv">
						<!--查询按钮 -->
						<g:button styleId="btnSearch" styleClass="search" type="button" text="<%=CommonMessageDomain.BUTTON_SEARCH %>"/>
						<!--重置按钮 -->
						<g:button styleId="btnReset" styleClass="reset" type="reset" text="<%= CommonMessageDomain.BUTTON_RESET %>" onclick="javascript:FormUtils.clearForm('userSearchForm');"/>
						<!--新增按钮 -->	
						<g:button styleId="btnNew"  styleClass="add" type="button" text="<%=CommonMessageDomain.BUTTON_NEW %>" />
						<!--保存按钮 -->
						<g:button styleId="btnSave" styleClass="save" type="button" text="<%=CommonMessageDomain.BUTTON_SAVE %>" />					
						<!--取消按钮 -->		
						<g:button styleId="btnCancel" styleClass="calcel" type="button" text="<%=CommonMessageDomain.BUTTON_CANCEL  %>"/>
						<!--删除按钮 -->
						<g:button styleId="btnDelete" styleClass="delete" type="button" text="<%=CommonMessageDomain.BUTTON_DELETE%>" />
					</div>
					<!--按钮-->			
				</html:form>
			 </div>
		  </div>
	<!--搜索模块-->
	
	<!--信息显示模块-->
	<div class="ModuleGrid">
	 <div class="ModuleGridBorder">
	<!-- 表格内容开始 -->
	<div id="divGrid1"></div>
	<!-- 表格内容 结束 -->
	<!--信息显示模块-->
	 </div>
	</div>
	
	<div>
	<!-- 编辑区域 -->
	<div class="ModuleEdit">
		<div class="ModuleEditBorder" id="userGridEditFormDiv" style="display:none;">
		 	<div class="ModuleSearchTitle"><g:label showColon="false" text="<%= CommonMessageDomain.TABEL_EDIT %>" /></div>
				<html:form action="/system/user.do?method=index" styleId="userGridEditForm">
					<table class="ModuleEditInput">
						<tr>
							<html:hidden property="keyId" styleId="keyId" />	
							<html:hidden property="userId" styleId="userId" />					
							<%-- <td ><g:label text="<%=UserDomain.firstName_full %>" /></td>
							<td><g:text property="firstName" /></td>
							<td><g:label text="<%=UserDomain.middleName_full %>" /></td>
							<td><g:text property="middleName" /></td>
							<td><g:label text="<%=UserDomain.lastName_full %>" /></td>
							<td><g:text property="lastName" /></td>		 --%>
						</tr>
						<tr>
							<td><g:label text="<%=UserDomain.userName_full %>" allowBlank="false"/></td>			
							<td><g:text property="userName" /></td>
							<td><g:label text="<%=UserDomain.userType_full %>" /></td>			
							<td><g:text property="userType" /></td>
							<td><g:label text="<%=UserDomain.password_full %>" allowBlank="false"/></td>			
							<td><g:text type="password"  property="password" /></td> 
						</tr>
						<tr>
							<td ><g:label text="<%=UserDomain.userCode_full %>" allowBlank="false"/></td>
							<td><g:text property="userCode" /></td>		
							<td><g:label text="<%=RoleDomain.officeCode_full %>" allowBlank="false"/></td>
							<td><g:combox kind="associate" styleId="comboxOffice" property="officeCode"/></td>
							<td><g:label text="<%=UserDomain.position_full %>" /></td>
							<td><g:text property="position" /></td>
						</tr>
						<tr>
							<td><g:label text="<%=UserDomain.mobile_full %>" allowBlank="false" /></td>
							<td><g:text property="mobile" isPhoneNumber="true"/></td>
							<td><g:label text="<%=UserDomain.email_full %>" allowBlank="false"/></td>
							<td><g:text isEmail="true" property="email" /></td>
							<%-- <td><g:label text="<%=UserDomain.fax_full %>" /></td>
							<td><g:text property="fax" /></td> --%>
						</tr>
					</table>
		
			</html:form>
		  </div>
		</div>
		<html:javascript formName="userGridEditForm" />
	<!-- 编辑区域 -->
	</div>
	<%@include file="/pages/common/bottom.jsp"%>
	</body>
</html>